package coml.jiyun.hp.sqlite;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView mMainRlv;
    /**
     * 增
     */
    private Button mBtnZeng;
    /**
     * 删
     */
    private Button mBtnDelete;
    private Button mBtnGai;
    /**
     * 查
     */
    private Button mBtnCha;
    private MySql mySql;
    private EditText mEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mMainRlv = (RecyclerView) findViewById(R.id.main_rlv);
        mMainRlv.setOnClickListener(this);
        mBtnZeng = (Button) findViewById(R.id.btn_zeng);
        mBtnZeng.setOnClickListener(this);
        mBtnDelete = (Button) findViewById(R.id.btn_delete);
        mBtnDelete.setOnClickListener(this);
        mBtnGai = (Button) findViewById(R.id.btn_gai);
        mBtnGai.setOnClickListener(this);
        mBtnCha = (Button) findViewById(R.id.btn_cha);
        mBtnCha.setOnClickListener(this);

        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i <= 10; i++) {
            list.add("贾小强" + i + ( 20+i));
        }

        mySql = new MySql(this);


        RlvAdapter rlvAdapter = new RlvAdapter(this, list);
        mMainRlv.setAdapter(rlvAdapter);
        mMainRlv.setLayoutManager(new LinearLayoutManager(this));

        mEt = (EditText) findViewById(R.id.et);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.main_rlv:
                break;
            case R.id.btn_zeng:
                mySql.insert(new Bean(mEt.getText().toString(),"18"));
                break;
            case R.id.btn_delete:
                mySql.delete(mEt.getText().toString());
                break;
            case R.id.btn_gai:
                mySql.upDate(mEt.getText().toString(),new Bean("贾小强","1"));
                break;
            case R.id.btn_cha:
                List<Bean> query = mySql.query();
                Log.d("111", "onClick: "+query);
                break;
        }
    }
}